package by.bsu.fami.greyscalecodefrontend.activities;

import by.bsu.fami.greyscalecodefrontend.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ResultActivity extends Activity {
	public static final String TEXT_TO_SHOW = "text";
	private String mTextToShow;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_result);
		mTextToShow = getIntent().getStringExtra(TEXT_TO_SHOW);
		if (mTextToShow == null) {
			mTextToShow = "";
		}
		setUp();		
	}

	private void setUp() {				
		Button openBrowser = (Button)findViewById(R.id.buttonOpenBrowser);		
		TextView textResultView = (TextView)findViewById(R.id.textResult);
		
		if (mTextToShow.startsWith("http") || mTextToShow.startsWith("www.")){
			openBrowser.setVisibility(View.VISIBLE);
		} else {
			openBrowser.setVisibility(View.GONE);
		}
		textResultView.setText(mTextToShow);		
	}
	
	public void onOpenBrowserClicked(View v){
		Intent intent = new Intent(Intent.ACTION_VIEW);
		String uri = mTextToShow;
		if (uri.startsWith("www.")){
			uri = "http://" + uri;
		}
		intent.setData(Uri.parse(uri));
		startActivity(intent);
	}
	
	public void onTextClicked(View v) {
		finish();
	}
}
